package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.util.Md5;
import com.cn.wanxi.util.Tool;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;

/**
 * 服务逻辑层
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    /**
     * 加密就是属于逻辑关系，所以这里需要对密码进行加密后，传入数据库
     *
     * @param model
     * @return
     */
    @Override
    public ResultModel add(UserModel model) {
        String mingwen = model.getPassword();
        String anwen = Md5.encoderByMd5(mingwen);
        model.setPassword(anwen);
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.add(model));
//        return resultModel;
        return ResultModel.getResult(userDao.add(model));
    }


    @Override
    public ResultModel update(UserModel model) {
        String mingwen = model.getPassword();
        String anwen = Md5.encoderByMd5(mingwen);
        model.setPassword(anwen);
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.update(model));
//        return resultModel;
        return ResultModel.getResult(userDao.update(model));
    }

    @Override
    public ResultModel delete(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.delete(model));
//        return resultModel;
        return ResultModel.getResult(userDao.delete(model));
    }

    @Override
    public ResultModel findById(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setData(userDao.findById(model));
//        return resultModel;
        return ResultModel.getResult(userDao.findById(model));
    }

    @Override
    public ResultModel findAll(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        以后查询所有的时候，会把总条数也一并查询出来
//        resultModel.setCount(userDao.getCount(model));
//        resultModel.setData(userDao.findAll(model));
//        return resultModel;
        return ResultModel.getResult(userDao.getCount(model), userDao.findAll(model));
    }

    @Override
    public ResultModel getCount(UserModel model) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setCount(userDao.getCount(model));
//        return resultModel;

        return ResultModel.getResult(userDao.getCount(model));
    }

    /**
     * 增加用户的时候，对密码进行加密
     * 所以，在登录的时候，去查询用户时，也应该做一个密码加密
     *
     * @param model
     * @return
     */
    @Override
    public ResultModel login(UserModel model) {
//        String mingwen = model.getPassword();
//        String anwen = Md5.encoderByMd5(mingwen);
//        model.setPassword(anwen);
//        必须进行加密
        model.setPassword(Md5.encoderByMd5(model.getPassword()));
        UserModel userModel = userDao.login(model);
        String result = "";
        if (userModel != null) {

            result = "success";
        }
        return ResultModel.getResult(result);
    }

}
